package com.j2205.hongzhiyuan.controller;

import com.alibaba.fastjson.JSON;
import com.j2205.hongzhiyuan.pojo.UserInfo;
import com.j2205.hongzhiyuan.service.DbBackService;
import com.j2205.hongzhiyuan.service.UserService;
import com.j2205.hongzhiyuan.service.impl.DbBackServiceImpl;
import com.j2205.hongzhiyuan.service.impl.UserServiceImpl;
import com.j2205.hongzhiyuan.vo.UserInfoVo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class UserInfoServlet extends BaseServlet{
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String username= req.getParameter("username");
        String password= req.getParameter("password");
        UserService userService=new UserServiceImpl();
        boolean b=userService.login(username,password);
        if(b){
            HttpSession session=req.getSession();
            session.setAttribute("user",username);
            resp.sendRedirect("sys/Main.html");
        }else{
            resp.sendRedirect("Login.html");
        }
    }
    protected void findUserByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();

        String  username = (String)session.getAttribute("user");

        UserService infoService = new UserServiceImpl();
        UserInfoVo vo = infoService.findUserInfoVoByName(username);
        System.out.println(vo);
        String json = JSON.toJSONString(vo);
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.write(json);
        writer.flush();
        writer.close();
    }
    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int usID = Integer.parseInt(req.getParameter("usID"));
        String userName = req.getParameter("txtA_user");
        String roleType = req.getParameter("ddlA_type");
        String password = req.getParameter("txtA_pass")==""?null:req.getParameter("txtA_pass");
        String realName = req.getParameter("txtA_name");
        int roleID = Integer.parseInt(req.getParameter("ddlA_post"));
        UserInfo userInfo = new UserInfo();
        userInfo.setUserID(usID);
        userInfo.setUserName(userName);
        userInfo.setPassword(password);
        userInfo.setRealName(realName);
        userInfo.setRoleID(roleID);
        userInfo.setRoleType(roleType);
        UserService userInfoService = new UserServiceImpl();
        boolean b = userInfoService.update(userInfo);
        if (b){
            resp.sendRedirect("sys/shezhi.html");
        }
    }
    public void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        session.setAttribute("currentUser",null);
        resp.sendRedirect("/Login.html");
    }
    public void backUp(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        DbBackService dbBackService = new DbBackServiceImpl();
        String username = (String) req.getSession().getAttribute("user");
        System.out.println(username);
        String ip = "127.0.0.1";
        boolean b = dbBackService.dbBack(username, ip);
        if (b){
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.write(JSON.toJSONString("success"));
            writer.flush();
            writer.close();
        }else {
            resp.sendRedirect("error.html");
        }
    }
    public void findUserById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int usID = Integer.parseInt(req.getParameter("usID"));
        UserService userInfoService = new UserServiceImpl();
        UserInfo userInfo =  userInfoService.findUserById(usID);
        resp.setContentType("application/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(userInfo));
        writer.flush();
        writer.close();
    }
    public void updateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

    }

}
